package com.cloud.tsp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cloud.api.domain.model.NoticeModel;
import com.cloud.core.exception.MyException;
import com.cloud.api.domain.model.ModelName;
import com.cloud.api.domain.model.VehicleModel;
import com.cloud.api.domain.model.VehicleModelDto;
import com.cloud.api.domain.tsp.Command;
import com.cloud.tsp.mapper.VehicleModelMapper;
import com.cloud.tsp.service.ICommandService;
import com.cloud.tsp.service.IEvModelInformationService;
import com.cloud.tsp.service.IOilModelInformationService;
import com.cloud.tsp.service.IVehicleModelService;
import org.springframework.stereotype.Service;

import jakarta.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author haohaibo
 * @since 2023-12-20
 */
@Service("vehicleModelService")
public class VehicleModelServiceImpl extends ServiceImpl<VehicleModelMapper, VehicleModel> implements IVehicleModelService {

    @Resource
    private VehicleModelMapper vehicleModelMapper;

    @Resource
    private ICommandService commandService;
    @Resource
    private IEvModelInformationService evModelInformationService;
    @Resource
    private IOilModelInformationService oilModelInformationService;

    @Override
    public List<VehicleModel> selectModelBrand(String type) {
        LambdaQueryWrapper<VehicleModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.isNull(VehicleModel::getParentId)
                .eq(VehicleModel::getType,type);
        return vehicleModelMapper.selectList(queryWrapper);
    }

    @Override
    public List<VehicleModel> selectModelByParentId(String parentId) {
        LambdaQueryWrapper<VehicleModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VehicleModel::getParentId,parentId);
        return vehicleModelMapper.selectList(queryWrapper);
    }

    @Override
    public List<VehicleModel> selectUrlListById(String modelId,String type) {
        List<VehicleModel> list = new ArrayList<>();
        if ("ev".equals(type)){
            list = vehicleModelMapper.selectUrlListByEvId(modelId);
        }else {
            list = vehicleModelMapper.selectUrlListByOilId(modelId);
        }
        return list;
    }

    @Override
    public List<VehicleModelDto> selectModelTreeList() {
        List<VehicleModelDto> list = new ArrayList<>();
        VehicleModelDto oilModel = new VehicleModelDto();
        VehicleModelDto evModel = new VehicleModelDto();
        List<VehicleModelDto> oilList = vehicleModelMapper.selectModelTreeList("oil");
        List<VehicleModelDto> evList = vehicleModelMapper.selectModelTreeList("ev");
        oilList.forEach(a -> a.setParentId("1"));
        evList.forEach(b -> b.setParentId("2"));
        oilModel.setId("1");
        oilModel.setModelName("油车");
        oilModel.setType("oil");
        oilModel.setChildren(oilList);
        evModel.setId("2");
        evModel.setModelName("新能源");
        evModel.setType("ev");
        evModel.setChildren(evList);
        list.add(oilModel);
        list.add(evModel);
        return list;
    }

    @Override
    public void insertModel(VehicleModel para) {
        if ("1".equals(para.getParentId()) || "2".equals(para.getParentId())) {
            para.setParentId(null);
        }
        int type = getType(para);
        String message = getMessage(type);
        List<VehicleModel> vehicleModels = vehicleModelMapper.selectList(new LambdaQueryWrapper<VehicleModel>().eq(VehicleModel::getModelName, para.getModelName()));
        for (VehicleModel vehicleModel : vehicleModels){
            int queryType = getType(vehicleModel);
            if (type == queryType){
                throw new MyException(message);
            }
        }
        this.saveOrUpdate(para);
    }

    public int getType(VehicleModel para){
        if (para.getParentId() == null){
            return 1;
        }
        List<VehicleModel> vehicleModels = vehicleModelMapper.selectList(null);
        // 找到层级
        return getLevel(para.getParentId(), vehicleModels);
    }

    public static int getLevel(String parentId, List<VehicleModel> models) {
        for (VehicleModel model : models) {
            if (Objects.equals(model.getId(), parentId)) {
                if (model.getParentId() == null) {
                    return 2;
                } else {
                    return getLevel(model.getParentId(), models) + 1;
                }
            }
        }
        return -1; // 如果找不到当前ID，则返回-1表示错误
    }

    public String getMessage(int type){
        switch (type){
            case 1:
                return "wds.exception.existBrandName";
            case 2:
                return "wds.exception.existSeriesName";
            case 3:
                return "wds.exception.existModelName";
            default:
                return "";
        }
    }

    @Override
    public void deleteById(String id) {
        LambdaQueryWrapper<VehicleModel>queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VehicleModel::getParentId,id);
        List<VehicleModel> list = vehicleModelMapper.selectList(queryWrapper);
        if (list.size() > 0){
            throw new MyException("wds.exception.deleteModel");
        }
        List<Command> commands = commandService.selectCommandByModel(id);
        if (commands.size()>0){
            throw new MyException("wds.exception.deleteModel");
        }
        vehicleModelMapper.deleteById(id);
    }

    @Override
    public ModelName selectParentNameById(String id) {
        return vehicleModelMapper.selectParentNameById(id);
    }

    //获取三级车型数据
    @Override
    public List<VehicleModel> getLastModelList() {
        return vehicleModelMapper.getLastModelList();
    }

    @Override
    public void updateModelName(VehicleModel para) {
        if ("1".equals(para.getParentId()) || "2".equals(para.getParentId())) {
            para.setParentId(null);
        }
        LambdaQueryWrapper<VehicleModel>queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(VehicleModel::getModelName,para.getModelName());
        queryWrapper.ne(VehicleModel::getId,para.getId());
        int type = getType(para);
        String message = getMessage(type);
        List<VehicleModel> list = vehicleModelMapper.selectList(queryWrapper);
        for (VehicleModel vehicleModel : list){
            int queryType = getType(vehicleModel);
            if (queryType == type){
                throw new MyException(message);
            }
        }

        VehicleModel vehicleModel = new VehicleModel();
        vehicleModel.setId(para.getId());
        vehicleModel.setModelName(para.getModelName());
        vehicleModelMapper.updateById(vehicleModel);
    }

    @Override
    public List<NoticeModel> selectNoticeModelByModel(String type) {
        return switch (type) {
            case "ev" -> evModelInformationService.selectNoticeModelByModel();
            case "oil" -> oilModelInformationService.selectNoticeModelByModel();
            default -> throw new MyException("wds.exception.lineRange");
        };
    }
}